﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Net;
using System.Net.Sockets;

namespace peonwar
{
    static class DevConsole
    {
        static public bool CheatsEnabled = false;
        static public bool Enabled = false;
        static public bool DevMode = false;
        static public bool PanicMode = false;
        static private string x = "thowait312312";
        static bool CanPressEnter = true;
        static bool CanPressUp = true;
        static bool CanPressDown = true;
        static int indexsortie = -1;
        static public List<char> charlist = new List<char> { };
        static float deplacement = -450f;
        static KbHandler console_input = new KbHandler(GameData.Font_Simple2, Color.White, new Vector2(20, 230), 400, "");
        static KeyboardState kbstate_ = Keyboard.GetState();
        static public Dictionary<Keys, string> BindList;
        static public bool AtLastOneBindedKey;


        //static string sortie = "";

        static List<string> output = new List<string> { "//Enter command here !" }; //le output de la console
        static List<string> memory = new List<string> { }; //Tout ce qui a été tapé dans la console

        static public string GetCategory(ref string arg)
        {
            string result = "";
            for (int i = 0; i < arg.Length; i++)
            {
                if (arg[i] == '_' || arg[i] == ' ') // Sort de la boucle, ampute la commande et retourne la jambe
                    break;
                else
                    if (arg[i] == ';')
                        return result; // Retourne l'argument de la commance
                    else
                        result += arg[i]; // Ajoute le caractere dans le buffer
            }
            arg = arg.Substring(result.Length + 1);
            return result;
        }

        static public string GetAction(string arg, Game game)
        {
            arg += ";";
            switch (GetCategory(ref arg))
            {
                #region Impulse
                case "impulse":
                    {
                        if (CheatsEnabled && !Game.CurrentGame.Online)
                        {
                            switch (GetCategory(ref arg))
                            {
                                case "101":
                                    {
                                        if (Game.gamestate == Gamestates.Playing)
                                        {
                                            Game.Impulse101();
                                            return "impulse " + GetCategory(ref arg);
                                        }
                                        else
                                            return "Impulse 101 can be used only while playing";
                                    }
                                case "203":
                                    {
                                        if (Game.gamestate == Gamestates.Playing)
                                        {
                                            if (Game.p1_player.PeonList != null)
                                                Game.p1_player.PeonList.Clear();
                                            if (Game.p2_opponent.PeonList != null)
                                                Game.p2_opponent.PeonList.Clear();
                                            return "impulse " + GetCategory(ref arg);
                                        }
                                        else
                                            return "Impulse 203 can be used only while playing";
                                    }
                                case "220":
                                    {
                                        if (Game.gamestate == Gamestates.Playing)
                                        {
                                        if (Research.cheat)
                                        {
                                            Research.cheat = false;
                                            return "Researchs locked";
                                        }
                                        else
                                        {
                                            Research.cheat = true;
                                            return "Researchs unlocked";
                                        }
                                        }
                                        else
                                            return "Impulse 220 can be used only while playing";
                                    }
                                default:
                                    return DefaultAction();
                            }
                        }
                        else
                            return "Can't use when cheats are disabled or when online";
                    }
                #endregion

                #region Game
                case "game":
                    switch (GetCategory(ref arg))
                    {
                        case "exit":
                            Environment.Exit(0); return "";
                        case "goto":
                            {
                                if (Game.gamestate != Gamestates.Playing || !Game.CurrentGame.Online)
                                    return Game.Goto(GetCategory(ref arg), game);
                                else return "Cannot goto when online";
                            }

                        #region Option
                        case "option":
                            {
                                if (Game.gamestate != Gamestates.Playing || !Game.CurrentGame.Online)
                                {
                                    switch (GetCategory(ref arg))
                                    {
                                        case "apply":
                                            SettingManager.SaveCurrentProfilFile(); return "Profil saved";
                                        case "reset":
                                            {
                                                Game.ResetOptions(game);
                                                return "Default values has been set";
                                            }

                                        #region Sound
                                        case "sound":
                                            {
                                                switch (GetCategory(ref arg))
                                                {
                                                    case "volume":
                                                        {
                                                            int result;
                                                            switch (GetCategory(ref arg))
                                                            {
                                                                case "quake":
                                                                    {
                                                                        if (Int32.TryParse(GetCategory(ref arg), out result) && result >= 0 && result <= 10)
                                                                        {
                                                                            SettingManager.CurrentProfil.QuakeVol = result;
                                                                            game.InitializeTextButtons();
                                                                            return "Quake Volume set to " + result;
                                                                        }
                                                                        else return "Quake Volume must be between 0 and 10";
                                                                    }
                                                                case "music":
                                                                    {
                                                                        if (Int32.TryParse(GetCategory(ref arg), out result) && result >= 0 && result <= 10)
                                                                        {
                                                                            SettingManager.CurrentProfil.MusicVol = result;
                                                                            MediaPlayer.Volume = (float)SettingManager.CurrentProfil.MusicVol / 10;
                                                                            game.InitializeTextButtons();
                                                                            return "Music Volume set to " + result;
                                                                        }
                                                                        else return "Quake Volume must be between 0 and 10";
                                                                    }
                                                                case "sound":
                                                                    {
                                                                        if (Int32.TryParse(GetCategory(ref arg), out result) && result >= 0 && result <= 10)
                                                                        {
                                                                            SettingManager.CurrentProfil.SoundVol = result;
                                                                            SoundEffect.MasterVolume = (float)SettingManager.CurrentProfil.SoundVol / 10;
                                                                            game.InitializeTextButtons();
                                                                            return "Sound Volume set to " + result;
                                                                        }
                                                                        else return "Sound Volume must be between 0 and 10";
                                                                    }
                                                                default:
                                                                    return DefaultAction();
                                                            }
                                                        }
                                                    case "quakestyle":
                                                        {
                                                            switch (GetCategory(ref arg))
                                                            {
                                                                case "male":
                                                                    SettingManager.CurrentProfil.Quake = 0;
                                                                    game.InitializeTextButtons();
                                                                    return "Quake Style set to male";
                                                                case "female":
                                                                    SettingManager.CurrentProfil.Quake = 15;
                                                                    game.InitializeTextButtons();
                                                                    return "Quake Style set to female";
                                                                case "disabled":
                                                                    SettingManager.CurrentProfil.Quake = -1;
                                                                    game.InitializeTextButtons();
                                                                    return "Quake Style set to disabled";
                                                                default:
                                                                    return "quakestyle can be either male, female or disabled";
                                                            }
                                                        }
                                                    case "panoramic":
                                                        switch (GetCategory(ref arg))
                                                        {
                                                            case "1":
                                                                SettingManager.CurrentProfil.Pan = true;
                                                                game.InitializeTextButtons();
                                                                return "Panoramic sounds enabled";
                                                            case "0":
                                                                SettingManager.CurrentProfil.Pan = false;
                                                                game.InitializeTextButtons();
                                                                return "Panoramic sounds disabled";
                                                            default:
                                                                return "Panoramic sounds can be either 1 (enabled) or 0 (disabled)";
                                                        }
                                                    default:
                                                        return DefaultAction();
                                                }
                                            }
                                        #endregion

                                        #region Video
                                        case "video":
                                            {
                                                switch (GetCategory(ref arg))
                                                {
                                                    case "particle":
                                                        {
                                                            switch (GetCategory(ref arg))
                                                            {
                                                                case "1":
                                                                    SettingManager.CurrentProfil.Particles = true;
                                                                    game.InitializeTextButtons();
                                                                    game.particleComponent.EnableEmitters(Game.CurrentMap);
                                                                    game.particleComponent.PauseEmitters();
                                                                    return "Particles enabled";
                                                                case "0":
                                                                    SettingManager.CurrentProfil.Particles = false;
                                                                    SettingManager.CurrentProfil.Blood = false;
                                                                    game.InitializeTextButtons();
                                                                    foreach (Emitter e in game.particleComponent.particleEmitterList)
                                                                        e.Active = false;
                                                                    return "Particles & Blood disabled (need restart)";
                                                                default:
                                                                    return "Particles can be either 1 (enabled) or 0 (disabled)";
                                                            }
                                                        }
                                                    case "blood":
                                                        {
                                                            switch (GetCategory(ref arg))
                                                            {
                                                                case "1":
                                                                    SettingManager.CurrentProfil.Blood = true;
                                                                    SettingManager.CurrentProfil.Particles = true;
                                                                    game.particleComponent.EnableEmitters(Game.CurrentMap);
                                                                    game.particleComponent.PauseEmitters();
                                                                    game.InitializeTextButtons();
                                                                    return "Blood & Particles enabled";
                                                                case "0":
                                                                    SettingManager.CurrentProfil.Blood = false;
                                                                    game.InitializeTextButtons();
                                                                    return "Blood disabled";
                                                                default:
                                                                    return "Blood can be either 1 (enabled) or 0 (disabled)";
                                                            }
                                                        }
                                                    case "fullscreen":
                                                        {
                                                            if (game.SwitchFullscreen())
                                                                return "Game is now in Fullscreen mode";
                                                            else
                                                                return "Game is now in Windowed mode";
                                                        }
                                                    default:
                                                        return DefaultAction();
                                                }
                                            }
                                        #endregion

                                        #region Game
                                        case "game":
                                            {
                                                switch (GetCategory(ref arg))
                                                {
                                                    case "language":
                                                        {
                                                            switch (GetCategory(ref arg))
                                                            {
                                                                case "english":
                                                                    GameData.CurrentLanguage = Language.English;
                                                                    SettingManager.CurrentProfil.Localization = Language.English;
                                                                    GameData.UpdateLanguage();
                                                                    game.InitializeTextButtons();
                                                                    return "Language set to ENGLISH";
                                                                case "french":
                                                                    GameData.CurrentLanguage = Language.French;
                                                                    SettingManager.CurrentProfil.Localization = Language.French;
                                                                    GameData.UpdateLanguage();
                                                                    game.InitializeTextButtons();
                                                                    return "Language set to FRENCH";
                                                                default:
                                                                    return "Unknown language";
                                                            }
                                                        }
                                                    case "keyboard":
                                                        {
                                                            switch (GetCategory(ref arg))
                                                            {
                                                                case "azerty":
                                                                    game.Options_azerty.ChangeText(GameData.GameString[152]);
                                                                    SettingManager.CurrentProfil.azerty = true;
                                                                    return "Keyboard set to azerty";
                                                                case "qwerty":
                                                                    game.Options_azerty.ChangeText(GameData.GameString[153]);
                                                                    SettingManager.CurrentProfil.azerty = false;
                                                                    return "Keyboard set to qwerty";
                                                                default:
                                                                    return "Keyboard can be either qwerty or azerty";
                                                            }
                                                        }
                                                    default:
                                                        return DefaultAction();
                                                }
                                            }
                                        #endregion

                                        default:
                                            return DefaultAction();
                                    }
                                }
                                else
                                {
                                    return "Can't change option while playing online";
                                }
                            }
                        #endregion

                        #region Profil
                        case "profil":
                            {
                                if (Game.gamestate != Gamestates.Playing && Game.gamestate != Gamestates.Lobby && !Game.CurrentGame.Online)
                                {
                                    switch (GetCategory(ref arg))
                                    {
                                        case "clearlist":
                                            {
                                                SettingManager.ClearList();
                                                game.UpdateConfig();
                                                game.InitializeTextButtons();
                                            }
                                            return "Profil list cleared";
                                        case "setdefault":
                                            SettingManager.SetDefaultProfil();
                                            return "Default profil has been set";
                                        default:
                                            return DefaultAction();
                                    }
                                }
                                else
                                {
                                    return "Can't modify profils while playing";
                                }
                            }
                        #endregion

                        default:
                            return DefaultAction();
                    }
                #endregion

                #region Give
                case "give":
                    {
                        if (CheatsEnabled && (Game.gamestate == Gamestates.Playing || Game.gamestate == Gamestates.Pause))
                        {
                            switch (GetCategory(ref arg))
                            {
                                case "money":
                                    {
                                        int result;
                                        if (Int32.TryParse(GetCategory(ref arg), out result))
                                        {
                                            if (Game.p1_player.Bank + result > 0)
                                            {
                                                Tools.BankManage(Game.p1_player, result);
                                                return "Bank edited";
                                            }
                                            else return "Can't have less than 0 money";

                                        }
                                        else
                                            return "Invalid argument";
                                    }
                                case "xp":
                                    {
                                        int result;
                                        if (Int32.TryParse(GetCategory(ref arg), out result))
                                        {
                                            if (result > 0 && result <= 10000)
                                            {
                                                Game.p1_player.Experience += Math.Min(result, 9999);
                                                return "XP edited";
                                            }
                                            else return "Xp must be between 1 and 10000";

                                        }
                                        else
                                            return "Invalid argument";
                                    }
                                case "cooldown":
                                    {
                                        int result;
                                        if (Int32.TryParse(GetCategory(ref arg), out result))
                                        {
                                            if (result > 0 && result <= 100)
                                            {
                                                Game.p1_player.Cooldown += result;
                                                if (Game.p1_player.Cooldown > 100)
                                                    Game.p1_player.Cooldown = 100;
                                                return "Cooldown edited";
                                            }
                                            else return "Cooldown must be between 1 and 100";

                                        }
                                        else
                                            return "Invalid argument";
                                    }
                                default:
                                    return DefaultAction();
                            }
                        }
                        else
                            return "Can't use \"give\" when cheats are disabled";
                    }
                #endregion

                #region Set
                case "set":
                    {
                        if (CheatsEnabled && (Game.gamestate == Gamestates.Playing || Game.gamestate == Gamestates.Pause))
                        {
                            switch (GetCategory(ref arg))
                            {
                                case "lvl":
                                    {
                                        int result;
                                        if (Int32.TryParse(GetCategory(ref arg), out result))
                                        {
                                            if (result >= 0 && result <= 6)
                                            {
                                                Game.p1_player.Level = result;
                                                Game.p1_player.Experience = 0;
                                                Game.p1_player.UnlockPower();
                                                return "LVL edited";
                                            }
                                            else return "Level must be between 0 and 5";

                                        }
                                        else
                                            return "Invalid argument";
                                    }
                                case "income":
                                    {
                                        int result;
                                        if (Int32.TryParse(GetCategory(ref arg), out result))
                                        {
                                            if (result >= 0 && result <= 999999)
                                            {
                                                Game.p1_player.Income = result;
                                                return "Income edited";
                                            }
                                            else return "Level must be between 0 and 999999";

                                        }
                                        else
                                            return "Invalid argument";
                                    }
                                default:
                                    return DefaultAction();
                            }
                        }
                            return "Can't use \"set\" when cheats are disabled";
                    }
                #endregion

                #region Say
                case "say":
                    {
                        if (Game.gamestate == Gamestates.Playing)
                            game.Game_chat.EnterChat(GetCategory(ref arg), Game.p1_player.TeamColor);
                        if (Game.CurrentGame.Online)
                        {
                            if (game.CurrentSession == Session.OnlineHOST)
                                game.Network_HostServer.Send(RequestType.Game_Chat, GetCategory(ref arg));
                            else
                                game.Network_Client.Send(RequestType.Game_Chat, GetCategory(ref arg));
                        }
                        return "You said : " + GetCategory(ref arg);
                    }
                #endregion

                #region Sv
                case "sv":
                    if (Game.gamestate != Gamestates.Playing || !Game.CurrentGame.Online)
                    {
                        switch (GetCategory(ref arg))
                        {
                            case "developer":
                                {
                                    if (GetCategory(ref arg) == x)
                                    {
                                        DevMode = true;
                                        return "Developer mode enabled";
                                    }
                                    else
                                        return "Invalid password";
                                }
                            case "cheats":
                                {
                                    CheatsEnabled = !CheatsEnabled;
                                    return "Cheats set to " + CheatsEnabled.ToString();
                                }
                            case "panic":
                                {
                                    PanicMode = !PanicMode;
                                    if (PanicMode)
                                        return "Panic mode enabled";
                                    else
                                        return "Panic mode disabled";
                                }
                            case "progress":
                                {
                                    if (DevMode)
                                    {
                                        int result;
                                        if (Int32.TryParse(GetCategory(ref arg), out result) && result > 0 && result <= 13)
                                        {
                                            SettingManager.CurrentProfil.Progress = result;
                                            CampaignManager.ActualMission = Math.Min(SettingManager.CurrentProfil.Progress, 12);
                                            CampaignManager.UpdateNodes();
                                            MapManager.UpdateMapList();
                                            return "Progress set to " + GetCategory(ref arg);
                                        }
                                        else
                                            return "Invalid argument";
                                    }
                                    else
                                        return "Can't use progress when developer mode is off";
                                }
                            default:
                                return DefaultAction();
                        }
                    }
                    else
                        return "Can't use Cheats/Dev/Panic mode online";
                #endregion

                #region Network
                case "network":
                    switch (GetCategory(ref arg))
                    {
                        case "sync":
                            {
                                if (Game.gamestate == Gamestates.Playing)
                                {
                                    if (game.CurrentSession == Session.OnlineHOST)
                                    {
                                        game.Network_HostServer.SendUnitList();

                                        return "Synchronized";
                                    }
                                    else
                                        return "The client can't make a synchronization";
                                }
                                else
                                    return "You can only synchronize during a play";
                            }
                        case "send":
                            {
                                if (DevMode)
                                {
                                    if (game.CurrentSession == Session.OnlineHOST && Game.gamestate == Gamestates.Playing)
                                    {
                                        game.Network_HostServer.Send(RequestType.ConsoleCommand, GetCategory(ref arg));
                                        return "Sent : " + GetCategory(ref arg);
                                    }
                                    return "Only HOST can send";
                                }
                                return "You can only send in Developer Mode";
                            }
                        case "sendscript":
                            {
                                if (DevMode)
                                {
                                    if (game.CurrentSession == Session.OnlineHOST && Game.gamestate == Gamestates.Playing)
                                    {
                                        List<string> cmds = SendScript(GetCategory(ref arg), game);
                                        if (cmds != null)
                                        {
                                            foreach (string s in cmds)
                                                game.Network_HostServer.Send(RequestType.ConsoleCommand, s);
                                            return "--Sent";
                                        }
                                        return "Sending error";
                                    }
                                    return "Only HOST can send script";
                                }
                                else
                                    return "You can only send script in developer Mode";
                            }
                        case "connect":
                            {
                                if (Game.gamestate != Gamestates.WaitClient && game.CurrentSession != Session.OnlineHOST && game.CurrentSession != Session.OnlineClient)
                                {
                                    try
                                    {
                                        game.Join_Failed = false;
                                        IPAddress ip = IPAddress.Parse(GetCategory(ref arg));
                                        game.Network_Client = new GameClient(ip, Network.FUNCTIONNALPORT, game);
                                        User.AdvertList.Clear();
                                        game.Join_IsConnecting = true;
                                        Game.gamestate = Gamestates.Join;
                                        game.player_ready = false;
                                        game.opponent_ready = false;
                                        return "Going to join menu";
                                    }
                                    catch
                                    {
                                        return "Invalid IP";
                                    }
                                }
                                else
                                    return "Can't join while waiting another player";
                            }
                        case "disconnect":
                            {
                                if (game.CurrentSession == Session.OnlineHOST || game.CurrentSession == Session.OnlineClient)
                                {
                                    if (game.CurrentSession == Session.OnlineHOST)
                                    {
                                        game.Network_HostServer.Disconnect(true, GameData.GameString[216], "");
                                    }
                                    else
                                    {
                                        game.Network_Client.Disconnect(true, GameData.GameString[217], "");
                                    }
                                    return "You disconnected the game";
                                }
                                else
                                    return "You aren't connected yet";
                            }
                        case "create":
                                {
                                    if (Game.gamestate != Gamestates.WaitClient && game.CurrentSession != Session.OnlineHOST && game.CurrentSession != Session.OnlineClient)
                                    {
                                        game.player_ready = false;
                                        game.opponent_ready = false;
                                        game.Network_HostServer = new GameServer(Network.FUNCTIONNALPORT, game);
                                        Game.gamestate = Gamestates.WaitClient;
                                        game.player_ready = false;
                                        game.opponent_ready = false;
                                        return "Server created";
                                    }
                                    return "Server already running";
                                }

                        default:
                            return DefaultAction();
                    }
                #endregion

                #region DevCommands
                case "clearconsole":
                    {
                        output.Clear();
                        return "Console cleared";
                    }
                case "showinfos":
                    {
                        InfoDisplay.Enabled = !InfoDisplay.Enabled;
                        return "Display infos set to " + InfoDisplay.Enabled.ToString();
                    }
                case "giveall":
                    {
                        if (DevMode)
                        {
                            for (int i = 0; i < AchievementManager.AchievementList.Count; i++)
                            {
                                AchievementManager.AchievementList[i].count = AchievementManager.AchievementList[i].limit;
                                AchievementManager.AchievementList[i].locked = false;
                                AchievementManager.AchievementSave();
                            }

                            return "All Achievements unlocked ";
                        }
                        else return "Developer mode not enabled";
                    }
                case "resetall":
                    {

                        for (int i = 0; i < AchievementManager.AchievementList.Count; i++)
                        {
                            AchievementManager.AchievementList[i].Reset();
                            AchievementManager.AchievementSave();
                        }

                        return "All Achievements reset";
                    }
                    
                #region Binds
                case "bind":
                    {
                        char c = Convert.ToChar(arg.Substring(0, 1));
                        string s = arg.Substring(2, arg.Length - 3);
                        return Bind(c, s) ? "Key binded" : "Binding error (wrong key?)";
                    }

                case "unbind":
                    {
                        char c = Convert.ToChar(arg.Substring(0, 1));
                        return UnBind(c) ? "Key Unbinded" : "Unbinding error (wrong key?)";
                    }
                case "unbindall":
                    {
                        BindList.Clear();
                        AtLastOneBindedKey = false;
                        return "All bindings has been removed";
                    }
                case "savebinds":
                    {
                        SaveBindings();
                        return "Current profil's bindings as been saved";
                    }
                case "showbinds":
                    {
                        foreach (KeyValuePair<Keys, string> pair in BindList)
                        {
                            AddOutput("\"" + pair.Value + "\" binded to : " + pair.Key.ToString());
                        }
                        return BindList.Count() + " binded keys";
                    }
                #endregion

                #region Script
                case "execute":
                    {
                        return ExecuteScript(GetCategory(ref arg), game);
                    }
                #endregion

                default:
                    return DefaultAction();
                #endregion
            }
        }

        static private string DefaultAction()
        {
            return "Unknow command : " + console_input.stock;
        }

        static public void AddOutput(string s)
        {
            output.Add(s);

            if (output.Count() > 11)
                output.RemoveAt(0);
        }

        static public void Draw(SpriteBatch spriteBatch, Game game)
        {
            KeyboardState kbstate_ = Keyboard.GetState();

            spriteBatch.Draw(GameTexture.Console, new Rectangle(0, (int)deplacement, GameTexture.Console.Width, GameTexture.Console.Height), Color.White);
            if (Enabled && deplacement < -170)
                deplacement += 19f;
            if (!Enabled && deplacement > -450f)
            {
                deplacement -= 19f;
                console_input.Reset();
            }
            if (Enabled & deplacement >= -170)
            {
                if (kbstate_.IsKeyDown(Keys.Enter) && CanPressEnter && console_input.stock != "") //Appuye sur enter et ça "active" ce qui a été tapé
                {
                    AddOutput(GetAction(console_input.stock, game));

                    if (!memory.Contains(console_input.stock))
                        memory.Add(console_input.stock);
                    console_input.Reset();
                    indexsortie = -1;
                    CanPressEnter = false;

                }
                if ((kbstate_.IsKeyUp(Keys.Enter) && !CanPressEnter))
                    CanPressEnter = true;

                console_input.Update(Game.highticks);
                console_input.DrawStock(spriteBatch);

                if (kbstate_.IsKeyDown(Keys.Up) && CanPressUp && indexsortie != memory.Count - 1) //Appuye sur Up et ça met les dernière string écrites
                {
                    CanPressUp = false;
                    indexsortie++;
                    console_input.stock = memory[memory.Count - indexsortie - 1];
                }
                if ((kbstate_.IsKeyUp(Keys.Up) && !CanPressUp))
                    CanPressUp = true;
                if (kbstate_.IsKeyDown(Keys.Down) && CanPressDown && indexsortie > 0) //Appuye sur Down et ça met les dernière string écrites
                {
                    CanPressDown = false;
                    indexsortie--;
                    console_input.stock = memory[memory.Count - indexsortie - 1];
                }
                if ((kbstate_.IsKeyUp(Keys.Down) && !CanPressDown))
                    CanPressDown = true;
            }

            for (int i = 0; i < output.Count; i++)
                spriteBatch.DrawString(GameData.Font_Simple2, output[i], new Vector2(10, 18 * (i) + (int)deplacement + 165), Color.Lime);
        }


        #region Bind
        // Systeme de bind
        public static void InitialiseBinding()
        {
            if (BindList == null)
            {
                BindList = new Dictionary<Keys, string>();
            }
            LoadBindings();
        }

        public static bool Bind(char n, string command)
        {
            try
            {
                Keys k = (Keys)char.ToUpper(n);
                if (BindList.ContainsKey(k))
                    BindList.Remove(k);
                BindList.Add(k, command);
                AtLastOneBindedKey = true;
            }
            catch
            {
                return false;
            }
            return true;
        }

        public static bool UnBind(char n)
        {
            try
            {
                Keys k = (Keys)char.ToUpper(n);
                BindList.Remove(k);
                if (BindList.Count() == 0)
                    AtLastOneBindedKey = false;
            }
            catch
            {
                return false;
            }
            return true;
        }

        public static void TryBind(KeyboardState kb, Game game)
        {
            foreach (KeyValuePair<Keys, string> pair in BindList)
            {
                if (kb.IsKeyDown(pair.Key))
                {
                    AddOutput(GetAction(pair.Value, game));
                    Game.CanPress = false;
                    break;
                }
            }
        }

        public static void SaveBindings()
        {
            List<string> bindings = new List<string>();
            foreach (KeyValuePair<Keys, string> pair in BindList)
                bindings.Add(pair.Key.ToString() + pair.Value);
            SettingManager.CurrentProfil.Bindings = bindings;
            SettingManager.SaveCurrentProfilFile();
        }

        public static void LoadBindings()
        {
            Dictionary<Keys, string> bindings = new Dictionary<Keys, string>();
            foreach (string s in SettingManager.CurrentProfil.Bindings)
            {
                Keys k = (Keys)char.ToUpper((Convert.ToChar(s.Substring(0, 1))));
                string cmd = s.Substring(1, s.Length - 1);
                bindings.Add(k, cmd);
            }
            BindList = bindings;
            AtLastOneBindedKey = BindList.Count() != 0;
        }
        #endregion

        #region Script
        public static string ExecuteScript(string path, Game game)
        {
            if (path.Substring(path.Length - 4, 4) != ".pws")
            {
                path = path + ".pws";
            }

            List<string> commandbuffer = new List<string>();

            try
            {
                StreamReader reader = new StreamReader(File.OpenRead(path));

                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    commandbuffer.Add(line);
                }

                reader.Close();
            }
            catch
            {
                return "Error, the file may doesn't exist";
            }

            AddOutput("--Executing : " + path);

            foreach (string s in commandbuffer)
            {
                if (s != "")
                    AddOutput(GetAction(s, game));
            }
            return "--End of script";
        }

        public static List<string> SendScript(string path, Game game)
        {
            if (path.Substring(path.Length - 4, 4) != ".pws")
            {
                path = path + ".pws";
            }

            List<string> commandbuffer = new List<string>();

            try
            {
                StreamReader reader = new StreamReader(File.OpenRead(path));

                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    commandbuffer.Add(line);
                }

                reader.Close();
            }
            catch
            {
                return null;
            }

            AddOutput("--Sending : " + path);

            return commandbuffer;
        }
        #endregion
    }
}
